package com.gxuwz.check.dao.admin.impl;

import com.gxuwz.check.base.BaseDao;
import com.gxuwz.check.dao.admin.ICourseDao;
import com.gxuwz.check.model.entity.SysCourseEntity;
import com.gxuwz.check.model.entity.SysTeacherEntity;
import com.gxuwz.check.model.vo.TeacherVO;
import com.gxuwz.check.util.PageBean;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

@Repository
public class CourseDaoImpl extends BaseDao implements ICourseDao {
    @Override
    public List<SysCourseEntity> list(int page) {
        Criteria criteria = getSession().createCriteria(SysCourseEntity.class);
        criteria.setFirstResult((page - 1)* new PageBean<>().getPageSize());
        criteria.setMaxResults(new PageBean<>().getPageSize());
        List<SysCourseEntity> list = criteria.list();
        SessionClose();
        return list;
    }

    @Override
    public void delete(Integer courseId) {
        String sql = "delete from sys_course where course_id = '"+courseId+"'";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    public void edit() {

    }

    @Override
    public void save(int chooseType,String courseName,String teachIdArray) {
        String sql = "insert into sys_course(course_name,choose_type,teacher_id) value('"+courseName+"','"+chooseType+"','"+teachIdArray+"')";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    public List<TeacherVO> listTeacherByCollegeId(int collegeId) {
        Criteria criteria = getSession().createCriteria(SysTeacherEntity.class);
        criteria.add(Restrictions.eq("collegeId",collegeId));
        List<SysTeacherEntity> list = criteria.list();
        SessionClose();
        List<TeacherVO> listVO = new ArrayList<>();
        for (int i = 0 ; i < list.size() ; i++){
            try {
                TeacherVO teacherVO = new TeacherVO();
                teacherVO.setTeacherId(list.get(i).getUserId()); //老师的工号(账号)
                teacherVO.setTeacherName(list.get(i).getTeacherName());
                listVO.add(teacherVO);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return listVO;
    }
}
